package com.hedl.elasticsearch.elasticsearch_client_test;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * ProjectName: stuy-elasticsearch
 * DateTime: 2023/9/7 21:54
 * Author: Big Watermelon
 * Description:
 */
public class StudyElasticsearchClient {


    public static void main(String[] args) {
        // URL and API key
        String serverUrl = "https://101.43.160.197:9200";
        String apiKey = "";

        // Create the low-level client
        RestClient restClient = RestClient.builder(HttpHost.create(serverUrl))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader("Authorization", "ApiKey " + apiKey)
                })
                .build();

        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());

        // And create the API client
        ElasticsearchClient esClient = new ElasticsearchClient(transport);
        try {
            esClient.indices().create(c->c.index("products"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    /*
    public static void main(String[] args) {
        RestClient restClient = RestClient.builder(new HttpHost("https://101.43.160.197",9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        try {
            client.indices().create(c->c.index("products"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

     */

}
